<?php
namespace Home\Controller;
use Think\Controller;

class OrdsureController extends Controller{

	public function ordsure(){
		$num = I("post.amount");		
		$row = I("post.row");
		foreach($num as $k=>$v){			
			M("showcar")->where(array("id"=>"{$k}"))->save(array("goods_num"=>"{$v}"));
		}
		$wh['id'] = array('in',$row);		
		$res = M('showcar')->where($wh)->select();
		//print_r($res);
		$shu = count($res);
		foreach($res as $v){
			$price = $v["goods_price"] * $v["goods_num"];
			$total +=$price;			
		}
		$_SESSION["price"] = $total;
		$_SESSION["morder"] = $res;
		$_SESSION["sid"] = array();
		foreach($res as $v){
			$_SESSION["sid"][] = $v["id"];
		}
		//print_r($_SESSION["sid"]);
		$uid = $_SESSION["uid"]=0;
		$this->assign("shu",$shu);
		$this->assign("res",$res);
		$this->assign("uid",$uid);
		$this->assign("total",$total);
		$re = M('address')->where("uid='{$uid}'")->order('type desc')->select();
		$geshu = 10-count($re);
		$this->assign("geshu",$geshu);
		$this->assign("resu",$re);
		$this->display();		
	}

	public function addrquery(){
		$fid = I('get.fid');
		$res = M('areas')->where("fid='{$fid}'")->select();
		$this->ajaxReturn($res);
	}

	public function addrchaxun(){
		$id = I("get.id");
		$rr = M("address")->find($id);
		$this->ajaxReturn($rr);
	}

	public function addressadd(){
		$tp = I("post.type");
		if($tp=1){
			M("address")->where(array("type"=>"1"))->save(array("type"=>"0"));
			$user = D('address');		
			$user->create($_POST);
			if($user->add()){
				$r['msg']='地址添加成功';
				$r['status']=true;
			}else{
				$r['msg']='地址添加失败';
				$r['status']=false;
			}
		}else{
			$user = D('address');		
			$user->create($_POST);
			if($user->add()){
				$r['msg']='地址添加成功';
				$r['status']=true;
			}else{
				$r['msg']='地址添加失败';
				$r['status']=false;
			}
		}
		
		$this->ajaxReturn($r);
	}

	public function addredit(){
		print_r($_POST);
		$tp = I("post.type");
		if($tp=1){
			M("address")->where(array("type"=>"1"))->save(array("type"=>"0"));
		}else{
			M("address")->where(array("type"=>"1"))->save(array("type"=>"0"));
		}
		$res = D('address');		
		$res->create($_POST);
		$r = array();
		if($res->save()){
			$r['msg']='地址修改成功';
			$r['status']=true;
		}else{
			$r['msg']='地址修改失败';
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}

	public function addressDel(){
		$id = I('get.id');
		$res = M('address')->delete($id);
		$r = array();
		if($res){
			$r['msg']='地址删除成功';
			$r['status']=true;
		}else{
			$r['msg']='地址删除失败';
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}

	public function ordSave(){
		$uid = $_SESSION['uid'];
		$pro = json_encode($_SESSION['morder']);
		//print_r($pro);
		$ordtime = time();
		$ordid = "D".time().rand(1000,9999);
		$_POST["pro"] = $pro;
		$_POST["ordtime"] = $ordtime;
		$_POST["uid"] = $uid;
		$_POST["ordid"] = $ordid;
		$res = D('order');		
		$res->create($_POST);
		$r = array();
		if($res->add()){
			$r['msg']='订单提交成功';
			$r['status']=true;
			$r['ordid']=$ordid;
		}else{
			$r['msg']='订单提交失败';
			$r['status']=false;
		}
		$this->ajaxReturn($r);
	}
	
}